package com.tct.worktimeboard.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tct.worktimeboard.config.DataSourceEnum;
import com.tct.worktimeboard.entity.UserInfo;
import com.tct.worktimeboard.log.DataSource;
import com.tct.worktimeboard.service.IUserInfoService;
import com.tct.worktimeboard.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liyongkui
 * @since 2021-02-25
 */
@RestController
@RequestMapping("/user-info")
public class UserInfoController {

    @Autowired
    IUserInfoService userInfoService;

    @DataSource(DataSourceEnum.DB2)
    @GetMapping("/getUserList")
    public JSONObject getUserList(UserInfo userInfo){
        if(userInfo.getPageNum()!=null&&userInfo.getPageSize()!=null){
            PageHelper.startPage(userInfo.getPageNum(),userInfo.getPageSize());
        }else{
            PageHelper.startPage(0,0);
        }
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        if(userInfo.getName()!=null&&!userInfo.getName().isEmpty()){
            wrapper.like("name",userInfo.getName());
        }
        List<UserInfo> list = userInfoService.list(wrapper);
        return JsonUtil.toJsonData(new PageInfo<>(list));
    }

    @GetMapping("/getHavePowerUserList")
    public JSONObject getHavePower(HttpServletRequest request){
        return userInfoService.getHavePowerByWorkTime(null);
    }

    @GetMapping("/getInitPowerUserList")
    public JSONObject getInitPowerUserList(HttpServletRequest request){
        return userInfoService.getInitPowerUserList(Long.valueOf(request.getHeader("userId")));
    }

    @GetMapping("/getUserInfo")
    public JSONObject getUserInfo(Long userId){
        return userInfoService.getUserInfo(userId);
    }

    @GetMapping("/getUserIdByAccount")
    public JSONObject getUserIdByAccount(String account){
        return userInfoService.getUserIdByAccount(account);
    }

}
